package com.feri.rbacsys.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.feri.rbacsys.dao.DepositoryDao;
import com.feri.rbacsys.entity.Returnorder;
import com.feri.rbacsys.mapper.BuyorderMapper;
import com.feri.rbacsys.service.ReturnorderService;
import com.feri.rbacsys.mapper.ReturnorderMapper;
import com.feri.rbacsys.vo.BuyAmounVo;
import com.feri.rbacsys.vo.R;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author 神经质的木子李
 * @description 针对表【t_returnorder】的数据库操作Service实现
 * @createDate 2023-03-28 15:31:29
 */
@Service
public class ReturnorderServiceImpl extends ServiceImpl<ReturnorderMapper, Returnorder>
        implements ReturnorderService {
    @Resource
    private ReturnorderMapper mapper;
    @Resource
    private DepositoryDao dao;
    @Resource
    private BuyorderMapper buyorderMapper;

    @Override
    public R addOrder(Returnorder returnorder) {
        if (returnorder != null) {
            //商品退货数量
            Integer returnStock = returnorder.getReturnStock();
            //商品最初库存数量
            int original = dao.selectCount(returnorder.getProductId());
            //商品采购的数量
            BuyAmounVo buyAmounVo = buyorderMapper.getAmout(returnorder.getProductId());
            int amount = buyAmounVo.getBuyAmount();
            if (returnStock <= (original + amount)) {
                //可以退货，添加退货记录
                if (mapper.insert(returnorder) > 0) {
                    return R.ok();
                }
            }
        }
        return R.fail();
    }

    @Override
    public R delOrder(int id) {
        return R.ok(mapper.deleteById(id));
    }

    @Override
    public R allOrder(int page, int size) {
        return R.ok(mapper.selectPage(new Page<>(), null));
    }

    @Override
    public R audito(int id) {
        if (mapper.audito(id) > 0) {
            return R.ok();
        }
        return R.fail();
    }
}




